{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# CSE 160: Worksheet 1 - if statements - Solutions\n", "\n", "Please try to answer the following questions without running any code initially.\n", "***" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Problem 4**\n", "\n", "\n", "Write if statements to check for the following:\n", "- determine whether a letter is in a given word (use \"in\") and if it is, print \"Found Letter!\" otherwise print \"Letter Not Found\"\n", "- determine whether a number is between 0 and 10 (inclusive) and if it is, print \"Valid Number\" otherwise print \"Invalid\"\n", "- determine if a word has more than 3 letters and if it is, print \"Long word\" otherwise print \"Short word\"\n", "\n", "**NOTE:** for the first task, you can arbitrarily choose a word and a letter. If you want more of a challenge, write a function that could be used for ANY word and ANY letter." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Letter Found!\n" ] } ], "source": [ "letter = 'a'\n", "name = \"Emilia\"\n", "if letter in name:\n", " print(\"Letter Found!\")\n", "else:\n", " print(\"Letter Not Found\")\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Letter Found!\n" ] } ], "source": [ "# We can easily turn this into a function:\n", "# NOTE: Better form would be for the function to RETURN, rather than PRINT its result, but it's ok for now.\n", "def find_letter(letter, word):\n", " if letter in word:\n", " print(\"Letter Found!\")\n", " else:\n", " print(\"Letter Not Found\")\n", " \n", "# We can now call the function.\n", "# NOTE: The notebook remembers the variables from the previous cell!\n", "\n", "find_letter(letter, name)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Valid Number\n" ] } ], "source": [ "val = 7\n", "if val >=0 and val <11:\n", " print(\"Valid Number\")\n", "else:\n", " print(\"Invalid\")" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Invalid\n" ] } ], "source": [ "# As a function:\n", "\n", "def valid_number(val):\n", " if val >=0 and val <11:\n", " print(\"Valid Number\")\n", " else:\n", " print(\"Invalid\")\n", " \n", "# Calling the function\n", "valid_number(27)\n" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Long word\n" ] } ], "source": [ "def long_or_short(word):\n", " if len(word) > 3:\n", " print(\"Long word\")\n", " else:\n", " print(\"Short word\")\n", " \n", "# Callling the function:\n", "long_or_short(\"computers\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Problem 5**\n", "\n", "Write a function that accomplishes the following:\n", " \n", "Given two int values, return their sum. \n", "However, if the two values are the same, then return double their sum.\n", "\n", "Given the following input, your function should return the following output:\n", "\n", "```\n", "sum_double(1, 2) → 3\n", "sum_double(3, 2) → 5\n", "sum_double(2, 2) → 8\n", "```\n", "***" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def sum_double(a, b):\n", " # Store the sum in a local variable\n", " sum = a + b\n", " \n", " # Double it if a and b are the same\n", " if a == b:\n", " sum = sum * 2\n", " return sum\n", "\n", "sum_double(3,3)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.1" } }, "nbformat": 4, "nbformat_minor": 2 }